$(function() {
	// 初始化地区选择器
	$("#list3").treemenu({
		jsondata : treedata,
		mousemove : false,
		deep : 2
	});

	var account = $("#account").val();
	if (account === "" || account === undefined || account === null) {
		openUserWindow();
	} else {
		// 获取选中的地区
		var locationText = $("#province").val() + "." + $("#city").val() + "."
				+ $("#district").val();
		// 重新赋值
		$("#list3").treemenu("setText", locationText);
	}

	$("#account").change(function() {
		$.get("/manager/proprietary/accountIsExist.do", {
			account : $(this).val()
		}, function(result) {
			if (result.code === 1 && result.data === true) {
				layer.tips("该账号已存在", "#account");
			}
			// else{
			// layer.tips("该账号可用", "#account");
			// }
		});
	});

	// 关联用户
	$("#lookUser").click(openUserWindow);

	// 修改密码
	$("#updatePwd").click(function() {
		layer.closeAll();
		_editerIndex = layer.open({
			id : "user",
			title : "修改密码",
			type : 1,
			skin : 'layui-layer-rim',
			area : [ '330px', '280px' ],
			content : $("#pwd_content"),
			success : function(e) {
				eConetnt = $(e);
				$("[id^='layui-layer-shade']").remove();
			}
		});
	});

	// 提交
	$("#submit").click(function() {
		var isOk = JQUtils.form.verifyForm($("#myForm"));
		if (isOk) {
			var text = $("#address").treemenu("getText");
			if (text === undefined || text === null || text === "") {
				layer.tips('请选择地区', '#address', {
					tips : [ 1, '#FF3366' ],
					time : 5000
				});
				return;
			}
			var locationText = text.split(".");
			if (locationText.length < 3) {
				layer.tips('地区选择不正确', '#list3', {
					tips : [ 1, '#FF3366' ],
					time : 5000
				});
				return;
			}
			// 重新给地区赋值
			$("#province").val(locationText[0]);
			$("#city").val(locationText[1]);
			$("#district").val(locationText[2]);
			layer.confirm('请确认表单是否已经填写无误？', {
				btn : [ '确认', '取消' ]
			// 按钮
			}, function() {
				$('#myForm').ajaxSubmit({
					type : "POST",
					dataType : "JSON",
					beforeSubmit : function() {
					},
					success : function(result) {
						if (result.code === 1) {
							layer.msg(result.msg);
							layer.close(_editerIndex);
						}
					},
					url : "/manager/proprietary/shopInfoEdit.do",
					error : function(XMLResponse) {
						layer.msg('操作失败！');
						// alert(XMLResponse.responseText);
					}
				});
			}, function() {

			});
		}
	});

	// 提交
	$("#submit_user").click(function() {
		var isOk = JQUtils.form.verifyForm($("#userForm"));
		if (isOk) {
			$('#userForm').ajaxSubmit({
				type : "POST",
				dataType : "JSON",
				beforeSubmit : function() {
				},
				success : function(result) {
					if (result.code === 1) {
						layer.msg(result.msg);
						layer.close(_editerIndex);
					}
				},
				url : "/manager/proprietary/userUpdate.do",
				error : function(XMLResponse) {
					layer.msg('操作失败！');
					// alert(XMLResponse.responseText);
				}
			});
		}
	});

	// 提交
	$("#submit_pwd").click(function() {
		var isOk = JQUtils.form.verifyForm($("#pwdForm"));
		var password = $("#password").val();
		var password2 = $("#password2").val();
		if (isOk) {
			if (password !== password2) {
				layer.tips("两次输入密码不一致", "#password2");
				return;
			}
			if (password.length < 6) {
				layer.tips("密码长度不许小于6位", "#password");
				return;
			}
			$('#pwdForm').ajaxSubmit({
				type : "POST",
				dataType : "JSON",
				beforeSubmit : function() {
				},
				success : function(result) {
					if (result.code === 1) {
						layer.msg(result.msg);
						layer.close(_editerIndex);
					}
				},
				url : "/manager/proprietary/updatepwd.do",
				error : function(XMLResponse) {
					layer.msg('操作失败！');
					// alert(XMLResponse.responseText);
				}
			});
		}
	});

	/**
	 * 重置
	 */
	$("#reset").click(function() {

	});
});

function openUserWindow() {
	layer.closeAll();
	_editerIndex = layer.open({
		id : "user",
		title : "关联用户",
		type : 1,
		skin : 'layui-layer-rim',
		area : [ '800px', '600px' ],
		content : $("#user_content"),
		success : function(e) {
			eConetnt = $(e);
			$("[id^='layui-layer-shade']").remove();
		}
	});
}
